package com.matt.service.impl;

import com.matt.domain.course.CoursePlanClasses;
import com.matt.domain.course.CoursePlanTopic;
import com.matt.foundation.DO.CoursePlanNurseryClassesDo;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.base.ClassesCoursePlanRepository;
import com.matt.repository.course.CoursePlanClassesRepository;
import com.matt.repository.course.CoursePlanTopicRepository;
import com.matt.repository.course.PrepareCourseRepository;
import com.matt.service.CoursePlanTopicService;
import com.matt.service.NoticeMessageService;
import com.matt.service.NurseryScheduleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/12/3
 * *******************************
 */
@Service
@Slf4j
public class CoursePlanTopicServiceImpl implements CoursePlanTopicService{
    @Autowired
    private CoursePlanTopicRepository coursePlanTopicRepository;

    @Autowired
    private CoursePlanClassesRepository coursePlanClassesRepository;

    @Autowired
    private ClassesCoursePlanRepository classesCoursePlanRepository;

    @Autowired
    private PrepareCourseRepository prepareCourseRepository;

    @Autowired
    private NurseryScheduleService nurseryScheduleService;

    @Autowired
    private NoticeMessageService noticeMessageService;

    @Transactional
    @Override
    public Boolean delete(String coursePlanTopicId) {
        deletedByCoursePlanTopicId(coursePlanTopicId);
        coursePlanTopicRepository.deleteById(coursePlanTopicId);
        return true;
    }

    @Transactional
    @Override
    public Boolean deletedByCoursePlanTopicId(String coursePlanTopicId) {
        List<CoursePlanClasses> planClassesList = coursePlanClassesRepository.findByCoursePlanTopicId(coursePlanTopicId);
        log.info("deleted CoursePlanClasses List Size :[{}]", planClassesList.size());
        return deleteCoursePlanClasses(planClassesList);
    }

    @Transactional
    @Override
    public Boolean deletedByCoursePlanTopicAndClasses(String coursePlanTopicId, String classesId) {
        List<CoursePlanClasses> planClassesList = coursePlanClassesRepository.findByCoursePlanTopicIdAndClassesId(coursePlanTopicId, classesId);
        log.info("deleted CoursePlanClasses List Size :[{}]", planClassesList.size());
        return deleteCoursePlanClasses(planClassesList);
    }

    private Boolean deleteCoursePlanClasses(List<CoursePlanClasses> planClassesList){
        List<String> coursePlanClassesIdList = planClassesList.stream().map(o->o.getId()).collect(Collectors.toList());
        Long prepareRecord = prepareCourseRepository.deleteByCoursePlanClassesIdIn(coursePlanClassesIdList);
        Long nurseryClassesRecord = coursePlanClassesRepository.deleteByIdIn(coursePlanClassesIdList);
        Long noticeRecord = noticeMessageService.deleteNurseryCourseByLinkIds(coursePlanClassesIdList);
        log.info("Change Classes target object , deleted for prepareRecord:[{}], nurseryClassesRecord:[{}], noticeRecord:[{}]", prepareRecord, nurseryClassesRecord, noticeRecord);
        return true;
    }

    @Transactional
    @Override
    public Boolean createClassesCourse(String coursePlanTopicId, String classesId, String areaId) {
        CoursePlanTopic coursePlanTopic = coursePlanTopicRepository.getOne(coursePlanTopicId);
        CoursePlanClasses coursePlanClasses = CoursePlanClasses.ofTopic(coursePlanTopic, areaId, classesId);
        coursePlanClassesRepository.save(coursePlanClasses);

        //修改托班已排好的课
        nurseryScheduleService.updateNurseryScheduleRecord(areaId, coursePlanClasses.getCourseDate(), classesId, coursePlanClasses.getId());
        return true;
    }

    @Transactional
    @Override
    public Boolean deleteAfterCourseForClasses(String classesId) {
        String now = DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
        List<CoursePlanNurseryClassesDo> classesDoList = classesCoursePlanRepository.findByClassIdAndCourseDate(classesId, now, null);
        List<String> classesCoursePlanIdList = classesDoList.stream().map(o -> o.getClassesCoursePlanId()).collect(Collectors.toList());
        Long prepareRecord = prepareCourseRepository.deleteByCoursePlanClassesIdIn(classesCoursePlanIdList);
        Long nurseryClassesRecord = coursePlanClassesRepository.deleteByIdIn(classesCoursePlanIdList);
        log.info("Change Classes target object , disable for prepareRecord:[{}], nurseryClassesRecord:[{}]", prepareRecord, nurseryClassesRecord);
        return true;
    }

}
